package com.itheima.app.controller;

import com.itheima.app.mapper.MovementManager;
import com.itheima.domain.mongo.Movement;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {


    @Autowired
    private MovementManager movementManager;

    //注意: 这里的参数不能使用@RequestBody来接收
    @PostMapping("/movements")
    public void saveMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        //调用manager保存
        movementManager.saveMovement(movement, imageContent);
    }

    //查看自己发布的动态
    @GetMapping("/movements/all")
    public PageBeanVo findMyMovement(Long userId,
                                     @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return movementManager.findMyMovement(userId, pageNum, pageSize);
    }


    //查询我的好友动态
    @GetMapping("/movements")
    public PageBeanVo findFriendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return movementManager.findFriendMovement(pageNum, pageSize);
    }

    //查询推荐的动态
   @GetMapping("/movements/recommend")
        public PageBeanVo findRecommendMovement(
                @RequestParam(value="page",defaultValue = "1") Integer pageNum,
                @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
                    return movementManager.findRecommendMovement(pageNum,pageSize);
        }

    //对动态进行点赞
    @GetMapping("/movements/{id}/like")
    public Integer movementListLike(@PathVariable("id") String movementId){
        return movementManager.movementLike(movementId);
    }

    //对动态进行取消点赞
    @GetMapping("/movements/{id}/dislike")
    public Integer movementDislike(@PathVariable("id") String movementId){
        return movementManager.movementDislike(movementId);
    }

    //对动态进行喜欢
    @GetMapping("/movements/{id}/love")
    public Integer movementListLove(@PathVariable("id") String movementId){
        return movementManager.movementLove(movementId);
    }

    //对动态进行取消喜欢
    @GetMapping("/movements/{id}/unlove")
    public Integer movementUnlove(@PathVariable("id") String movementId){
        return movementManager.movementUnlove(movementId);
    }

    //单条动态
    @GetMapping("/movements/{id}")
    public MovementVo singletonMovement(@PathVariable("id") String movementId){
        return movementManager.findMovementById(movementId);
    }

    //查询动态评论列表
    @GetMapping("/comments")
    public PageBeanVo findMovementComment( String movementId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {
        return movementManager.findMovementComment(movementId,pageNum,pageSize);
    }

    //对动态发表语言评论
    @PostMapping("/comments")
    public  Integer saveComment(@RequestBody Map<String, String> map){
        //1.接受参数
        String movementId = map.get("movementId");//动态id
        String comment = map.get("comment");//评论内容

        //2.调用manager保存评论
        return movementManager.saveComment(movementId,comment);
    }


    //动态评论点赞
    @GetMapping("/comments/{id}/like")
    public Integer movementCommentLike(@PathVariable("id") String commentId){
        return  movementManager.movementCommentLike(commentId);
    }

    //取消动态评论点赞
    @GetMapping("/comments/{id}/dislike")
    public Integer mevementCommentDisLike(@PathVariable("id") String commentId){
        return  movementManager.movementCommentDisLike(commentId);
    }
}
